// ValidationAspects
// Written by Mike Saunders
// Released under Microsoft Public License (Ms-PL)
using System;
using System.Globalization;
using NUnit.Framework;

namespace TestValidationAspects
{
	public static class Test
	{
		public static void Throws(Action action)
		{
			try
			{
				action.Invoke();
			}
			catch (Exception)
			{
				return;
			}
			Assert.Fail("Exception was not thrown.");
		}

		public static void Throws<TException>(Action action)
		{
			try
			{
				action.Invoke();
			}
			catch (Exception e)
			{
				Assert.IsAssignableFrom(typeof(TException), e);
				return;
			}
			Assert.Fail(string.Format(CultureInfo.InvariantCulture, "Exception [type:{0}] was not thrown.", typeof(TException)));
		}

		public static void Throws<TException, TInnerException>(Action action)
		{
			try
			{
				action.Invoke();
			}
			catch (Exception e)
			{
				Assert.IsAssignableFrom(typeof(TException), e);
				Assert.IsAssignableFrom(typeof(TInnerException), e.InnerException);
				return;
			}
			Assert.Fail(string.Format(CultureInfo.InvariantCulture, "Exception [type:{0}] was not thrown.", typeof(TException)));
		}

		public static void Throws<TException>(Action action, string message)
		{
			try
			{
				action.Invoke();
			}
			catch (Exception e)
			{
				Assert.IsAssignableFrom(typeof(TException), e);
				Assert.AreEqual(message, e.Message);
				return;
			}
			Assert.Fail(string.Format(CultureInfo.InvariantCulture, "Exception [type:{0}] was not thrown.", typeof(TException)));
		}

		public static void Throws<TException, TInnerException>(Action action, string message)
		{
			try
			{
				action.Invoke();
			}
			catch (Exception e)
			{
				Assert.IsAssignableFrom(typeof(TException), e);
				Assert.IsAssignableFrom(typeof(TInnerException), e.InnerException);
				Assert.AreEqual(message, e.Message);
				return;
			}
			Assert.Fail(string.Format(CultureInfo.InvariantCulture, "Exception [type:{0}] was not thrown.", typeof(TException)));
		}
	}
}